<?php

namespace app\admin\controller;
use app\admin\model\MemberModel;
use app\admin\model\MemberGroupModel;
use app\admin\model\AreaModel;
use think\Db;

class Member extends Base
{
    //*********************************************学历*********************************************//
    /**
     * [offer 学历]
     * @author 【朱沪宁】
     */
    public function offer(){

        $key = input('key');
        $map = [];
        if($key&&$key!==""){
            $map['offer_name'] = ['like',"%" . $key . "%"];
        }

        $offer = new MemberGroupModel();
        $Nowpage = input('get.page') ? input('get.page'):1;
        //$limits = config('list_rows');
        $limits = 52;
        $count = $offer->getAllCount($map);         //获取总条数
        $allpage = intval(ceil($count / $limits));  //计算总页面
        $lists = $offer->getAll($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('val', $key);
        if(input('get.page')){
            return json($lists);
        }

        return $this->fetch();
    }

    /**
     * [add_offer 添加学历]
     * @author 【朱沪宁】
     */
    public function add_offer()
    {
        if(request()->isAjax()){
            $param = input('post.');
            $offer = new MemberGroupModel();
            $flag = $offer->insertGroup($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch();
    }


    /**
     * [edit_offer 编辑学历]
     * @author 【朱沪宁】
     */
    public function edit_offer()
    {
        $offer = new MemberGroupModel();
        if(request()->isPost()){
            $param = input('post.');
            $flag = $offer->editGroup($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('offer',$offer->getOne($id));
        return $this->fetch();
    }


    /**
     * [del_offer 删除学历]
     * @author 【朱沪宁】
     */
    public function del_offer()
    {
        $id = input('param.id');
        $offer = new MemberGroupModel();
        $flag = $offer->delGroup($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    //*********************************************会员列表*********************************************//
    /**
     * 会员列表
     * @author 【朱沪宁】
     */
    public function index(){

        $map = [];
        $key = input('key');   
        if($key&&$key!=="")
        {
            $map['nickname|mobile'] = ['like',"%" . $key . "%"];
        }
        $offer_id = input('offer_id');
        if($offer_id&&$offer_id!=="")
        {
            $map['think_member.offer_id'] = $offer_id;
        }
        $province_id = input('province_id');
        if($province_id&&$province_id!=="")
        {
            $map['think_member.home_province'] = $province_id;
        }
        $member = new MemberModel();       
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = $member->getAllCount($map);//计算总页面
        $allpage = intval(ceil($count / $limits));       
        $lists = $member->getMemberByWhere($map, $Nowpage, $limits);
        foreach ($lists as &$v){
            if($v['habit']){
                $v['habit'] = implode(',',model('TagModel')->where(['id'=>['in',$v['habit']]])->column('name'));
            }
        }
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        $this->assign('offer_id', $offer_id);
        $this->assign('province_id', $province_id);
        if(input('get.page'))
        {
            return json($lists);
        }
        $map = [];
        $offers = new MemberGroupModel();
        $this->assign('offers',$offers->getGroup($map));

        $areas = new AreaModel();
        $this->assign('areas',$areas->getArea(0));
        return $this->fetch();
    }


    public function select_member(){
     
        $map['closed'] = 0;//0未删除，1已删除
        $key = input('key');   
        if($key&&$key!=="")
        {
            $map['account|nickname|mobile'] = ['like',"%" . $key . "%"];          
        }
        $offer_id = input('offer_id');   
        if($offer_id&&$offer_id!=="")
        {
            $map['think_member.offer_id'] = $offer_id;          
        }
        $province_id = input('province_id');
        if($province_id&&$province_id!=="")
        {
            $map['think_member.home_province'] = $province_id;
        }
        $member = new MemberModel();       
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = $member->getAllCount($map);//计算总页面
        $allpage = intval(ceil($count / $limits));       
        $lists = $member->getMemberByWhere($map, $Nowpage, $limits);   
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        $this->assign('province_id', $province_id);
        $this->assign('offer_id', $offer_id);
        if(input('get.page'))
        {
            return json($lists);
        }
        
        $map = [];
        $offers = new MemberGroupModel();

        $this->assign('offers',$offers->getGroup($map));
        
        $org = new OrganizationModel();
        $this->assign('org',$org->getAllOrganizations(""));
        
        return $this->fetch();
    }

    /**
     * 添加会员
     * @author 【朱沪宁】
     */
    public function add_member()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $param['password']='123456';
            $param['password'] = md5(md5($param['password']) . config('auth_key'));
            $member = new MemberModel();
            $flag = $member->insertMember($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $map=[];
        $offers = new MemberGroupModel();
        $this->assign('offers',$offers->getGroup($map));

        $areas = new AreaModel();
        $this->assign('areas',$areas->getArea(0));
        return $this->fetch();
    }


    /**
     * 编辑会员
     * @author 【朱沪宁】
     */
    public function edit_member()
    {
        $member = new MemberModel();
        if(request()->isAjax()){
            $param = input('post.');
//            print_r($param);die;
            if($param['habit']){
                $param['habit'] = implode(',',$param['habit']);
            }
            $flag = $member->editMember($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $map=[];
        $id = input('param.id');
        $info = $member->getOneMember($id);
        if($info['habit']){
            $info['habit'] = explode(',',$info['habit']);
        }
        $offer = new MemberGroupModel();
        $areas = new AreaModel();
        $this->assign([
            'member' => $info,
            'offer' => $offer->getGroup($map),
            'areas'=>$areas->getArea(0),
            'citys'=>$areas->getArea($info['home_province']),
            'habits'=>Db('tag')->field('id,name')->select(),
        ]);
        return $this->fetch();
    }


    /**
     * 删除会员
     * @author 【朱沪宁】
     */
    public function del_member()
    {
        $id = input('param.id');
        $member = new MemberModel();
        $flag = $member->delMember($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * 会员状态
     * @author 【朱沪宁】
     */
    public function member_status()
    {
        $id = input('param.id');
        $status = Db::name('member')->where('id',$id)->value('status');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('member')->where('id',$id)->setField(['status'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已禁止']);
        }
        else
        {
            $flag = Db::name('member')->where('id',$id)->setField(['status'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    
    }
    public function member_area()
    {
        $id = input('param.id');
        $areas = new AreaModel();
        $citys = $areas->getArea($id);
        if($citys){
            return json($citys);
        }

    }
}